package com.app.controller.mybatis;

import com.app.dao.mybatis.BUserDao;
import com.app.entity.User;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * BUserController
 *
 * @author nanxin.chen
 * @date 2015/9/28
 */
@Controller
@RequestMapping(value = "/buser")
public class BUserController {
    public static final Logger log = Logger.getLogger(BUserController.class);

    @Autowired
    private BUserDao bUserDao;

    @RequestMapping(value = "/list")
    public String getUserList(HttpServletRequest request) {
        try {
            log.info("buser/list");
            List<User> uList = bUserDao.getUserList();
            for (User u : uList) {
                log.info(u);
            }
            return "success";
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
            return "error";
        }
    }


    @Transactional
    @RequestMapping(value = "/getuser")
    public String getUser(HttpServletRequest request) {
        try {
            log.info("buser/getuser");
            int id = Integer.valueOf(request.getParameter("id"));
            log.info("parameter: id=" + id);
            User user = bUserDao.getUser(id);
            log.info(user);
            return "success";
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
            return "error";
        }
    }


    public BUserDao getBUserDao() {
        return bUserDao;
    }

    public void setBUserDao(BUserDao bUserDao) {
        this.bUserDao = bUserDao;
    }
}
